#include<bits/stdc++.h>
using namespace std;
const ll N = 100010;
typedef long long ll;

ll fac[N], inv[N];

ll quick_pow(ll a, ll n, ll p) {
    ll ans = 1;
    a %= p;
    while (n) {
        if (n & 1)   ans = (ans * a) % p;
        a = (a * a) % p;
        n >>= 1;
    }
    return ans;
}

ll C(ll a, ll b, ll p) {
    if (b > a)return 0;
    return ((fac[a] * inv[b]) % p * inv[a - b] % p);
}

ll Lucas(ll a, ll b, ll p) {
    if (b == 0) return 1;
    return C(a % p, b % p, p) * Lucas(a / p, b / p, p) % p;
}

ll main() {
    ll T; cin >> T;
    while (T--) {
        ll a, b, p; cin >> a >> b >> p;
        fac[0] = inv[0] = 1;
        for (ll i = 1;i <= p;i++) {
            fac[i] = (fac[i - 1] * i) % p;
            inv[i] = quick_pow(fac[i], p - 2, p);
        }
        cout << Lucas(a + b, a, p) << endl;
    }
}
